﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class MessaggeController : Controller
    {
        private MvcApplication2Context db = new MvcApplication2Context();

      

        public ActionResult Index(string filter)
        {
            var user = ModelHelper.GetUser(db);
            var msg =
                db.UserMessages.Include(t => t.Exercise).Where(m => m.Type == UserMessageType.GenericMessage).Where(
                    m1 => m1.Owner.UserProfileId == user.UserProfileId || m1.UsersAllowed.Any(m2 => m2.UserProfileId == user.UserProfileId));

            return View(msg.ToList());
        }

        //
        // GET: /Messagge/Details/5

        public ActionResult Details(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            return View(threadmessage);
        }

        //
        // GET: /Messagge/Create

        public ActionResult Create()
        {
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status");
            return View();
        }

        //
        // POST: /Messagge/Create

        [HttpPost]
        public ActionResult Create(UserMessage threadmessage)
        {
            if (ModelState.IsValid)
            {
                db.UserMessages.Add(threadmessage);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // GET: /Messagge/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // POST: /Messagge/Edit/5

        [HttpPost]
        public ActionResult Edit(UserMessage threadmessage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(threadmessage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // GET: /Messagge/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            return View(threadmessage);
        }

        //
        // POST: /Messagge/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            db.UserMessages.Remove(threadmessage);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}